package pers.amos.edu.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class CommonExceptionHandler {

    private static Logger logger = LoggerFactory.getLogger(CommonExceptionHandler.class);

    @ExceptionHandler(EducationException.class)
    @ResponseBody
    public ResponseEntity<ExceptionResult> educationException(EducationException e) {
        logger.error("catch exception:{}", e.getExceptionEnum().getMsg());
        ExceptionEnum em = e.getExceptionEnum();
        e.printStackTrace();
        return ResponseEntity.status(em.getCode()).body(new ExceptionResult(em));
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<ExceptionResult> customException(Exception e) {
        logger.error("catch exception:{}", e.getCause());
        e.printStackTrace();
        return ResponseEntity.status((500)).body(new ExceptionResult(ExceptionEnum.SYSTEM_ERROR));
    }
}
